package cn.xunjie.basic.controller;

import cn.xunjie.basic.dto.LoginDto;
import cn.xunjie.basic.dto.WechatDto;
import cn.xunjie.basic.exception.BusinessException;
import cn.xunjie.basic.service.ILoginService;
import cn.xunjie.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try{
        Map<String, Object> map =  loginService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatDto dto){
        try{
            return loginService.wechat(dto);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }

    }
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatDto dto){
        try{
            Map<String, Object> map = loginService.binder(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }

    }


}
